package com.huawang.ecolink.apps.common;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

public class FileHandlerUtils {
    private FileHandlerUtils() {
    }

    /**
     * 判断路径是否存在 不存在创建包
     *
     * @param path
     * @throws IOException
     */
    public static void checkfileExist(String path) throws IOException {
        if (!Files.exists(Paths.get(path))) {
            Files.createDirectories(Paths.get(path));
        }
    }


    public static void writeContentToFile(String path, String name, String content) throws IOException {
        Writer write = null;
        try {
            checkfileExist(path);
            Path pathUrl = Paths.get(path + File.separator + name);
            write = Files.newBufferedWriter(pathUrl, StandardCharsets.UTF_8);
            write.write(content);
            write.flush();
        }  finally {
            if(Objects.nonNull(write)){
                write.close();
            }
        }

    }

}
